package com.gzw.dms.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;

import com.gzw.dms.MyApplication;
import com.gzw.dms.R;
import com.gzw.dms.base.BaseActivity;
import com.gzw.dms.bean.ShippingOrderInfo;
import com.gzw.dms.bean.UserInfo;
import com.gzw.dms.utils.HttpUtils;
import com.gzw.dms.utils.JsonUtil;
import com.gzw.dms.utils.T;
import com.gzw.dms.utils.UrlUtils;

import java.util.HashMap;
import java.util.Map;

public class MonitMapActivity extends BaseActivity {
    private WebView webView;
    private Intent mIntent;
    private Toolbar toolbar;
    private Context mContext;
    private ProgressBar pg1;
    private EditText et_number,et_ordernum;
    private Button select;
    private String order_goid,order_num,order_id;
    private View pb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_monit_map);

    }

    @Override
    protected void initViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        et_number = (EditText) findViewById(R.id.et_number);
        et_ordernum = (EditText) findViewById(R.id.et_order_num);
        select = (Button) findViewById(R.id.btn_select);
        pb = findViewById(R.id.include_pb);

        webView = (WebView) findViewById(R.id.webView);
        pg1=(ProgressBar) findViewById(R.id.progressBar1);


    }

    @Override
    protected void initData() {
        mIntent = getIntent();
        mContext = this;
        toolbar.setTitle("节点");
        order_goid = "";
        order_num = "";
        order_id = "";
        getMap();

    }


    @Override
    protected void initEvents() {
        toolbar.setNavigationOnClickListener(new mOnClick());
        select.setOnClickListener(new mOnClick());

    }

    class mOnClick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
                case R.id.btn_select:
                    order_num = et_ordernum.getText().toString().trim();
                    order_goid = et_number.getText().toString().trim();
                    FPOrderTask fpOrderTask = new FPOrderTask();
                    fpOrderTask.execute();
                    break;
                default:
                    finish();
                    break;
            }
        }
    }
    private void getMap(){
        Map<String, Object> map = new HashMap<>();
        map.put("type", "1");
        map.put("order_id",order_id );
        String json = JsonUtil.toJson(map);
//        String url = "http://192.168.5.20:8080/scms/app.do?method=appToOrderMapInfo&json="+json;
//        String url = "http://139.129.216.151:8088/scms/app.do?method=appToOrderMapInfo&json="+json;
//        String url = "http://139.129.216.151:56788/scms/app.do?method=appToOrderMapInfo&json="+json;
        String url = "http://139.129.216.151/app.do?method=appToOrderMapInfo&json="+json;




        webView.loadUrl(url);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });


        WebSettings seting=webView.getSettings();
        seting.setJavaScriptEnabled(true);//设置webview支持javascript脚本
        seting.setJavaScriptCanOpenWindowsAutomatically(true);
        seting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        seting.setBuiltInZoomControls(true); // 显示放大缩小 controler
        seting.setSupportZoom(true); // 可以缩放
        seting.setDefaultZoom(WebSettings.ZoomDensity.CLOSE);// 默认缩放模式
        seting.setUseWideViewPort(true);
        seting.setLoadWithOverviewMode(true);

        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // TODO 自动生成的方法存根

                if(newProgress==100){
                    pg1.setVisibility(View.GONE);//加载完网页进度条消失
                }
                else{
                    pg1.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    pg1.setProgress(newProgress);//设置进度值
                }

            }
        });

    }

    public class FPOrderTask extends AsyncTask<String, Void, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {
            pb.setVisibility(View.VISIBLE);

        }

        protected String doInBackground(String... params) {
            Map<String,  Object> map = new HashMap<>();
            UserInfo userInfo = MyApplication.getUserInfo();
            map.put("userInfo", userInfo);
            map.put("shiping_order_num",order_num);
            map.put("shiping_order_goid",order_goid);
            String json = JsonUtil.toJson(map);
            return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("appGetShippingOrderId"), "json=" + json);
        }

        @Override
        protected void onPostExecute(final String result) {

            pb.setVisibility(View.GONE);

            if (isNull(result)) {
                T.showShort(mContext, "连接服务器异常");
            } else {
                if (result.indexOf("errorMsg") > 0) {
                    String errorMsg = JsonUtil.getMsg(result, "errorMsg");
                    T.showShort(mContext, errorMsg);
                } else {
                    String successMsg = JsonUtil.getMsg(result, "successMsg");
                    ShippingOrderInfo shippingOrderInfo = JsonUtil.getPerson(successMsg, ShippingOrderInfo.class);
                    order_id = shippingOrderInfo.getShiping_order_id();
                    getMap();
                }
            }

        }
    }

}
